#include "settingsdialog.h"

SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent) 
{
	setupUi(this);

	m_Settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "DevProxy", "DevProxy", this);

	connect( ButtonBox, SIGNAL(accepted()), this, SLOT(updateConfig()) );
	connect( ButtonBox, SIGNAL(rejected()), this, SLOT(reloadConfig()) );
	
	connect( LineCB, SIGNAL(stateChanged(int)), this, SLOT(setLineEnabled(int)) );

	reloadConfig();
}

void SettingsDialog::updateConfig()
{
	m_Settings->setValue(KEY_DEBUG, debugMode());
	m_Settings->setValue(KEY_AUTOCONNECT, autoconnect());
	m_Settings->setValue(KEY_FONT,  font());
	m_Settings->setValue(KEY_NEEDLINE, needLine());
	m_Settings->setValue(KEY_LINE, line());
	
	accept();
}

void SettingsDialog::reloadConfig()
{
	DebugModeCB->setChecked( m_Settings->value(KEY_DEBUG, false).toBool() );
	AutoConnectCB->setChecked( m_Settings->value(KEY_AUTOCONNECT, false).toBool() );	
	FontComboBox->setCurrentFont( m_Settings->value( KEY_FONT, QFont("Monospace") ).value<QFont>() );
	LineCB->setChecked( m_Settings->value(KEY_NEEDLINE, false).toBool() );
	LineEdit->setText( m_Settings->value(KEY_LINE, "").toString() );
	
	setLineEnabled( LineCB->isChecked() );
	
	reject();
}

bool SettingsDialog::debugMode()
{
	return DebugModeCB->isChecked();
}

bool SettingsDialog::autoconnect()
{
	return AutoConnectCB->isChecked();
}

QFont SettingsDialog::font()
{
	return FontComboBox->currentFont();
}

bool SettingsDialog::needLine()
{
	return LineCB->isChecked();
}

QString SettingsDialog::line()
{
	return LineEdit->text();
}

void SettingsDialog::setLineEnabled(int state)
{
	if (state == Qt::Unchecked) LineEdit->setEnabled(false);
	else LineEdit->setEnabled(true);
}

// kate: space-indent off; tab-indents on; indent-width 4; indent-mode cstyle;
